<template>
  <div class="main-wrapper">
    <div class="filter-container">
      <el-input v-model="listQuery.name" placeholder="名称" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-select v-model="listQuery.status" placeholder="状态" clearable class="filter-item">
        <el-option v-for="item in statusOptions" :key="item.key" :label="item.statusName" :value="item.key" />
      </el-select>
      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">查询</el-button>
      <el-button class="filter-item" type="primary" icon="el-icon-edit" @click="handleCreate">添加</el-button>
    </div>

    <el-table v-loading="listLoading" :data="list" border fit highlight-current-row>
      <el-table-column label="标题" align="center">
        <template slot-scope="{row}">
          {{ row.title }}
        </template>
      </el-table-column>
      <el-table-column label="信息内容" align="center">
        <template slot-scope="{row}">
          {{ row.content }}
        </template>
      </el-table-column>
      <el-table-column label="信息来源" align="center">
        <template slot-scope="{row}">
          {{ row.fromId }}
        </template>
      </el-table-column>
      <el-table-column label="信息来源名称" align="center">
        <template slot-scope="{row}">
          {{ row.fromName }}
        </template>
      </el-table-column>
      <el-table-column label="信息时间" align="center">
        <template slot-scope="{row}">
          <span>{{ row.infoTime | timeFilter('{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center">
        <template slot-scope="{row}">
          <el-tag :type="row.status | styleFilter()">{{ row.status | statusFilter() }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center">
        <template slot-scope="{row}">
          <span>{{ row.createTime | timeFilter('{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="{row}">
          <el-button size="mini" type="success" @click="handleDetail(row)">详情</el-button>
          <el-button type="primary" size="mini" @click="handleUpdate(row)">修改</el-button>
          <el-button v-if="row.status!=0" size="mini" type="danger" @click="delIndustryInfo(row)">删除</el-button>
          <el-button v-if="row.status!=1" size="mini" type="success" @click="reuseIndustryInfo(row)">恢复</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="fetchIndustryInfoList" />

    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form ref="dataForm" :rules="rules" :model="industryInfoData" label-position="left" label-width="90px">
        <el-form-item label="ID">
          <el-input v-model="industryInfoData.id" placeholder="请填写ID" />
        </el-form-item>
        <el-form-item label="标题">
          <el-input v-model="industryInfoData.title" placeholder="请填写标题" />
        </el-form-item>
        <el-form-item label="信息内容">
          <el-input v-model="industryInfoData.content" placeholder="请填写信息内容" />
        </el-form-item>
        <el-form-item label="信息来源">
          <el-input v-model="industryInfoData.fromId" placeholder="请填写信息来源" />
        </el-form-item>
        <el-form-item label="信息来源名称">
          <el-input v-model="industryInfoData.fromName" placeholder="请填写信息来源名称" />
        </el-form-item>
        <el-form-item label="信息时间">
          <el-input v-model="industryInfoData.infoTime" placeholder="请填写信息时间" />
        </el-form-item>
        <el-form-item label="状态 0 不展示 1 展示">
          <el-select v-model="industryInfoData.status" class="filter-item" placeholder="请选择">
            <el-option v-for="item in statusOptions" :key="item.key" :label="item.statusName" :value="item.key" />
          </el-select>
        </el-form-item>
        <el-form-item label="创建时间">
          <el-input v-model="industryInfoData.createTime" placeholder="请填写创建时间" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="dialogStatus==='create'?createIndustryInfo():updateIndustryInfo()">提交</el-button>
      </div>
    </el-dialog>

    <el-dialog title="详情" :visible.sync="dialogDetailVisible">
      <el-form ref="dataForm" :model="industryInfoData" label-position="left" label-width="90px">
        <el-form-item label="ID">
          <span>{{ industryInfoData.id }}</span>
        </el-form-item>
        <el-form-item label="标题">
          <span>{{ industryInfoData.title }}</span>
        </el-form-item>
        <el-form-item label="信息内容">
          <span>{{ industryInfoData.content }}</span>
        </el-form-item>
        <el-form-item label="信息来源">
          <span>{{ industryInfoData.fromId }}</span>
        </el-form-item>
        <el-form-item label="信息来源名称">
          <span>{{ industryInfoData.fromName }}</span>
        </el-form-item>
        <el-form-item label="信息时间">
          <span>{{ industryInfoData.infoTime | timeFilter('{y}-{m}-{d} {h}:{i}') }}</span>
        </el-form-item>
        <el-form-item label="状态 0 不展示 1 展示">
          <el-tag :type="industryInfoData.status | styleFilter()">{{ industryInfoData.status | statusFilter() }}</el-tag>
        </el-form-item>
        <el-form-item label="创建时间">
          <span>{{ industryInfoData.createTime | timeFilter('{y}-{m}-{d} {h}:{i}') }}</span>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">关闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getIndustryInfoList, addIndustryInfo, editIndustryInfo, editIndustryInfoStatus } from '@/api/industryInfo'
import { parseTime } from '@/utils'
import Pagination from '@/components/Pagination'

export default {
  name: 'IndustryInfo',
  components: {
    Pagination
  },
  filters: {
    styleFilter(style) {
      const styleMap = { 1: 'success', 0: 'danger' }
      return styleMap[style]
    },
    timeFilter(time, format) {
      return parseTime(time, format)
    },
    statusFilter(value) {
      const statusMap = { 0: '无效', 1: '有效' }
      return statusMap[value]
    }
  },
  data() {
    return {
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20
      },
      statusOptions: [
        { key: 0, statusName: '无效' },
        { key: 1, statusName: '有效' }
      ],
      industryInfoData: {},
      dialogFormVisible: false,
      dialogDetailVisible: false,
      dialogStatus: '',
      textMap: {
        update: '修改',
        create: '添加'
      },
      rules: {}
    }
  },
  created() {
    this.fetchIndustryInfoList()
  },
  methods: {
    fetchIndustryInfoList() {
      this.listLoading = true
      getIndustryInfoList(this.listQuery).then(response => {
        this.list = response.data.items
        this.total = response.data.total
        setTimeout(() => {
          this.listLoading = false
        }, 1.5 * 1000)
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.fetchIndustryInfoList()
    },
    resetIndustryInfoData() {
      this.industryInfoData = {}
    },
    handleDetail(row) {
      this.industryInfoData = Object.assign({}, row)
      this.dialogDetailVisible = true
    },
    handleCreate() {
      this.resetIndustryInfoData()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    createIndustryInfo() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          addIndustryInfo(this.industryInfoData).then((response) => {
            this.fetchIndustryInfoList()
            this.dialogFormVisible = false
            this.$notify({
              title: '成功',
              message: '创建成功',
              type: 'success',
              duration: 2000
            })
          })
        }
      })
    },
    handleUpdate(row) {
      this.industryInfoData = Object.assign({}, row)
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    updateIndustryInfo() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          const tempData = Object.assign({}, this.industryInfoData)
          editIndustryInfo(tempData).then(() => {
            for (const v of this.list) {
              if (v.id === this.industryInfoData.id) {
                const index = this.list.indexOf(v)
                this.list.splice(index, 1, this.industryInfoData)
                break
              }
            }
            this.dialogFormVisible = false
            this.$notify({
              title: '成功',
              message: '更新成功',
              type: 'success',
              duration: 2000
            })
          })
        }
      })
    },
    delIndustryInfo(row) {
      row.status = 0
      this.updateIndustryInfoStatus(row, 0).then(() => {
        this.$notify({
          title: '成功',
          message: '删除成功',
          type: 'success',
          duration: 2000
        })
        const index = this.list.indexOf(row)
        this.list.splice(index, 1)
      })
    },
    reuseIndustryInfo(row) {
      row.status = 1
      this.updateIndustryInfoStatus(row, 1).then(() => {
        this.$notify({
          title: '成功',
          message: '数据恢复成功',
          type: 'success',
          duration: 2000
        })
        const index = this.list.indexOf(row)
        this.list.splice(index, 1)
      })
    },
    updateIndustryInfoStatus(row, status) {
      row.status = status
      editIndustryInfoStatus(row.lid, status).then(() => {
        this.industryInfoData = Object.assign({}, row)
        this.industryInfoData.status = status
        for (const v of this.list) {
          if (v.id === this.industryInfoData.id) {
            const index = this.list.indexOf(v)
            this.list.splice(index, 1, this.industryInfoData)
            break
          }
        }
      })
      this.$message({
        message: '操作成功',
        type: 'success'
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.main-wrapper {
  margin: 8px;
  .filter-container {
    padding: 10px 0;
    .filter-item {
      width: 200px;
      margin-right: 10px;
    }
  }
}
</style>
